﻿using System;
using System.Linq;
using ITS.DAL.Concrete.EF;
using ITS.Core.Domain.Users;
using ITS.Infrastructure.Helpers;
using ITS.DAL.Users;

namespace ITS.DAL.Concrete.EF.Users
{
    public class EFUserAccountRepository : IUserAccountRepository
    {
        private EnterpriseContext context = new EnterpriseContext();
        public IQueryable<User> Users
        {
            get
            {
                return context.Users;
            }
        }
        public User Get(Guid ID)
        {
            return context.Users.SingleOrDefault(o => o.UserID == ID);
        }
        public User GetByUsername(string username)
        {
            return context.Users.SingleOrDefault(o => o.UserName == username);
        }
        public User UserByID(Guid ID)
        {
            return context.Users.SingleOrDefault(x => x.UserID == ID);
        }
        public User UserByUsername(string username)
        {
            return context.Users.SingleOrDefault(x => x.UserName == username);
        }
        public IQueryable<Role> Roles
        {
            get
            {
                return context.Roles;
            }
        }

        public bool HasRole(Guid UserID, Guid RoleID)
        {
            User user = UserByID(UserID);
            Role role = RoleByID(RoleID);
            return user.Roles.Contains(role);

        }

        public Role RoleByID(Guid ID)
        {
            return context.Roles.SingleOrDefault(x => x.RoleID == ID);
        }

        public void Save(User user)
        {
            if (user.UserID.Equals(Guid.Empty))
            {
                user.UserID = Guid.NewGuid();
            }
            context.Users.Add(user);
            context.SaveChanges();
        }
        public void UpdateUser(User user)
        {
            context.Entry(user).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public User Authenticate(string email, string nonencryptedpass)
        {
            string encryptedPassword = !string.IsNullOrEmpty(nonencryptedpass) ? CryptographyHelper.ToMD5(nonencryptedpass) : string.Empty;
            return (from usr in context.Users
                    where usr.UserName.Equals(email) && usr.Password.Equals(encryptedPassword)
                    select usr).FirstOrDefault();
        }


        public void Delete(User user)
        {
            context.Users.Remove(user);
            context.SaveChanges();
        }


        public bool IsExisting(string username)
        {
            User user = context.Users.SingleOrDefault(o => o.UserName == username);
            if (user != null)
                return true;
            else return false;
        }


        public void SaveUserExtended(UserExtended userExtended)
        {
            context.UserExtendeds.Add(userExtended);
            context.SaveChanges();
        }
        public void UpdateUserExtended(UserExtended userExtended)
        {
            context.Entry(userExtended).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }
    }
}
